package com.woniuxy.spring;

import com.woniuxy.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    //static Map<String,Person> map = new HashMap();
    static Properties properties = new Properties();

   static{
        //1.读取配置文件

        //2.获取一个InputStream
        InputStream resourceAsStream = BeanFactory.class.getResourceAsStream("beans.properties");
        //3.读取配置文件
        try {
            properties.load(resourceAsStream);
            //4. 获取配置文件中所有的key
            for (String key : properties.stringPropertyNames()) {
                //遍历后的key，来获取对应的value，这个value就是个全限定类名
                String property = properties.getProperty(key);
                Person person =  (Person) Class.forName(property).newInstance();
                //map.put(key,person);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }



    public Person getBean(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        return (Person) Class.forName(properties.getProperty(name)).newInstance();
    }
}
